package streamforce.crypto.user;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import streamforce.crypto.event.TransformedCTEventAll;
import streamforce.crypto.event.UserInitAggEvent;
import streamforce.crypto.event.UserInitEvent;
import streamforce.crypto.message.PolicyMessage;
import streamforce.crypto.message.UserHandshakeMessage;
import streamforce.crypto.policies.Policy;
import streamforce.crypto.user.subscriber.UserSelectAllSimple;
import streamforce.crypto.user.subscriber.UserSelectPriceSimple;
import streamforce.crypto.user.subscriber.UserSumAllAgg1;
import streamforce.crypto.user.subscriber.UserSumPriceAgg1;

/**
 * Connecting to the server.
 * 
 * It only has to listen to new messages: initilization and
 * then decryption
 *
 */
public class EPUserClientAgg1 extends EPUserClientSimple{
	private final Log log = LogFactory.getLog(EPUserClientAgg1.class); 
		
	public EPUserClientAgg1(int ui, int type){
		super(ui,type); 	
		int[] ids = {this.userId};
		this.uhm = new UserHandshakeMessage(ids, PolicyMessage.AGG_QUERY_1);
	}
		
	@Override
	protected void initializeUser(UserInitAggEvent pmu){				
		if (this.type==EPUserClientSimple.SELECT_PRICE)
			this.userPolicy = new UserSumPriceAgg1(this.cryptoHandler);
		else if (this.type == EPUserClientSimple.SELECT_ALL)
			this.userPolicy = new UserSumAllAgg1(this.cryptoHandler);
		this.userPolicy.initUser_slidingWindow_Aggregate1(this.userId,
				pmu.getPubK()[0], pmu.gettK()[0], pmu.getDecK()[0], pmu.getWindowSize(),
				pmu.getwK()[0]);		
		log.info("user "+pmu.getUserId()+" initialized , windowSize = "+pmu.getWindowSize()); 
	}
		
	public static void main(String[] args){
		int id = new Integer(args[0]).intValue();
		(new EPUserClientAgg1(id,EPUserClientAgg1.SELECT_ALL)).start();
	}
}
